﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp3
{

//    添加3个类，分别实现 IComparer<T>接口，实现对Student类的三个字段的排序。
//1、学生类：学号、姓名、年龄
//2、请选择：1、添加学生信息。2、查询学生信息。
//3、重复的学号不能添加。
//4、查询学生信息功能中有：1、查询所有（按学号排序）2、查询所有（按姓名排序），2、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出
     class Program
    {
        public static object Students { get; private set; }

        static void Main(string[] args)
        {
            Student s1 = new Student(1,"黄子韬",25);
            Student s2 = new Student(2,"张艺兴",25);
            Student s3 = new Student(3,"彭昱畅",25);
            Student s4 = new Student(4,"菅田将暉",25);
            Hashtable ht = new Hashtable();

            List<Student> students = new List<Student> { s1, s2, s3, s4 };

            ht.Add(s1.id, s1);
            ht.Add(s2.id, s2);
            ht.Add(s3.id, s3);
            ht.Add(s4.id, s4);
            text(students,ht);
        }

        private static void text(List <Student> Students, Hashtable ht)
        {
            Console.WriteLine("请选择：1、添加学生信息  2、查询学生信息");
            int a = int.Parse(Console.ReadLine());
            switch (a)
            {
                case 1:
                    choose1(ref Students,ht);

                    break;

                case 2:
                    choose2( Students, ht);
                    break;
            }
        }

        private static void choose1(ref List<Student> students, Hashtable ht)
        {
            Console.WriteLine("请输入学号：");
            int id = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入姓名：");
            string name = Console.ReadLine();
            Console.WriteLine("请输入年龄");
            int nianlin = int.Parse(Console.ReadLine());
            Student a = new Student(id,name,nianlin);
            if (ht.Contains(id))
            {
                Console.WriteLine("该学号已存在！！");
                choose1(ref students, ht);
                text(students, ht);
            }
            else
            {
                students.Add(a);
                ht.Add(a.id, a);
                text(students, ht);
            }
        }

        private static void choose2( List<Student> students, Hashtable ht)
        {
            Console.WriteLine("请选择：1、查询所有（按学号排序） 2、查询所有（按姓名排序） 3、查询所有（按年龄排序）4、按学号查询  5、退出 ");
            int b = int.Parse(Console.ReadLine());
            switch (b)
            {
                case 1:
                    students.Sort(new Idsort());
                    foreach (var s in students)
                    {
                        Console.WriteLine(s.ToString());
                    }
                    break;
                case 2:
                    students.Sort(new Namesort());
                    foreach (var s in students)
                    {
                        Console.WriteLine(s.ToString());
                    }
                    break;
                case 3:
                    students.Sort(new Nianlinsort());
                    foreach (var s in students)
                    {
                        Console.WriteLine(s.ToString());
                    }
                    break;
                case 4:
                    Console.WriteLine("请输入学号：");
                    int i = int.Parse(Console.ReadLine());

                    if (ht.Contains(i))
                    {
                        Console.WriteLine(ht[i]);
                        choose2(students, ht);
                    }

                    else
                    {
                        Console.WriteLine("该学生不存在!!!");
                    }
                    break;
                case 5:
                    Console.WriteLine("程序结束！！");
                    break;
                default:
                    break;
            }
            }
    }
}
